<template>
  <div class="app-container">
    <div class="filter-container query">
      <el-form :model="listQuery" label-position="right" label-width="120px">
        <el-row :gutter="10">
          <el-col :xs="12" :sm="8" :lg="6">
            <el-form-item label="编码" prop="code">
              <el-input v-model="listQuery.code" @keyup.enter.native="handleFilter" />
            </el-form-item>
          </el-col>
          <el-col :xs="12" :sm="8" :lg="6">
            <el-button type="primary" icon="el-icon-search" size="small" @click="handleFilter">
              搜索
            </el-button>
            <el-button class="filter-item" style="margin-left: 10px;" type="success" size="small" icon="el-icon-circle-plus-outline" @click="handleCreate">
              新增
            </el-button>
            <el-button type="info" icon="el-icon-refresh" size="small" @click="handleReset">
              重置
            </el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>

    <el-table
      :loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;height: 100%;"
      size="small"
      @sort-change="sortChange"
    >
      <el-table-column label="操作" align="center" width="180">
        <template slot-scope="{row}">
          <el-button size="mini" type="primary" @click="handleUpdate(row)">
            编辑
          </el-button>
          <el-button size="mini" type="danger" @click="handleDelete(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="编码" prop="code" sortable="custom" width="250" header-align="center" align="left">
        <template slot-scope="scope">
          <span>{{ scope.row.code }}</span>
        </template>
      </el-table-column>
      <el-table-column label="Json信息" prop="text" width="1000" header-align="center" align="left" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.text }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" prop="createAt" width="200" header-align="center" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.createAt }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNo" :limit.sync="listQuery.pageNoSize" size="small" @pagination="getList" />

    <el-dialog :title="dialogFormVisible[dialogStatus]" :visible.sync="dialogFormVisible" top="10vh" width="520px" :close-on-click-modal="false" :close-on-press-escape="false" center>
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="right" label-width="120px" class="form-center">
        <el-form-item label="编码" prop="code">
          <el-input v-model="temp.code" maxlength="64" />
        </el-form-item>
        <el-form-item label="Json信息" prop="text">
          <el-input v-model="temp.text" type="textarea" autosize />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button :loading="submiting" type="primary" @click="dialogStatus==='create'?createData():updateData()">
          确认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getBaseJsonTextList, createBaseJsonText, updateBaseJsonText, deleteBaseJsonText } from '@/api/base/jsontext'

import Pagination from '@/components/Pagination'

export default {
  name: 'BaseJsonText',
  components: { Pagination },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: false,
      listQuery: {
        pageNo: 1,
        pageSize: 20,
        code: '',
        sortField: undefined,
        sortOrder: undefined
      },
      temp: {
        id: null,
        code: '',
        text: ''
      },
      dialogTitleMap: {
        create: '新建Json信息',
        update: '更新Json信息'
      },
      dialogFormVisible: false,
      dialogStatus: '',
      rules: {
        code: [{ required: true, max: 64, message: '编码最大长度不能超过64个字符', trigger: 'change' }],
        text: [{ required: true, message: 'Json字符串不能为空', trigger: 'change' }]
      },
      submiting: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getBaseJsonTextList(this.listQuery).then(response => {
        this.list = response.data
        this.total = response.total
        this.listLoading = false
      }, response => {
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.pageNo = 1
      this.getList()
    },
    handleReset() {
      this.listQuery = {
        code: ''
      }
    },
    sortChange(data) {
      const { prop, order } = data
      this.sortByField(prop, order)
    },
    sortByField(prop, order) {
      if (order === 'ascending') {
        this.listQuery.sortField = prop
        this.listQuery.sortOrder = 'asc'
      } else if (order === 'descending') {
        this.listQuery.sortField = prop
        this.listQuery.sortOrder = 'desc'
      } else {
        this.listQuery.sortField = undefined
        this.listQuery.sortOrder = undefined
      }
      this.handleFilter()
    },
    resetTemp() {
      this.temp = {
        id: null,
        code: '',
        text: ''
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.submiting = true
          createBaseJsonText(this.temp).then(response => {
            this.submiting = false
            this.dialogFormVisible = false
            this.getList()
            this.$message({
              message: 'JSON信息保存成功!',
              type: 'success'
            })
          }, () => {
            this.submiting = false
          })
        }
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.submiting = true
          updateBaseJsonText(this.temp).then(response => {
            this.submiting = false
            this.getList()
            this.dialogFormVisible = false
            this.$message({
              message: 'JSON信息更新成功!',
              type: 'success'
            })
          }, () => {
            this.submiting = false
          })
        }
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleDelete(row) {
      this.$msgbox({
        type: 'warning',
        title: '确认',
        message: '确认删除JSON[' + row.code + ']?',
        showCancelButton: true,
        showClose: false,
        closeOnPressEscape: false,
        closeOnClickModal: false,
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        beforeClose: (action, instance, done) => {
          if (action === 'confirm') {
            instance.confirmButtonLoading = true
            deleteBaseJsonText(row.id).then((response) => {
              this.getList()
              this.$message({
                message: 'JSON[' + row.code + ']删除成功!',
                type: 'success'
              })
              done()
              instance.confirmButtonLoading = false
            }, (response) => {
              done()
              instance.confirmButtonLoading = false
            })
          } else {
            done()
          }
        }
      })
    }

  }
}
</script>
